package com.boot.init;

import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;

import java.util.Map;

@Repository
public class StockRepository {
    private final StringRedisTemplate stringRedisTemplate;

    public StockRepository(StringRedisTemplate stringRedisTemplate) {
        this.stringRedisTemplate = stringRedisTemplate;
    }

    public void initStock(Map<String, String> map) {
        stringRedisTemplate.opsForValue().multiSet(map);
    }

    public int getStock(String productId) {
        String key = "product:stock:" + productId;
        String value = stringRedisTemplate.opsForValue().get(key);
        return value == null ? 0 : Integer.parseInt(value);
    }
}
